/*
 * Copyright 2025 The JA-SIG Collaborative. All rights reserved.
 * distributed with this file and available online at
 */
package com.lap.auth.application.tenant;

import com.lap.auth.application.tenant.assembler.TenantAssembler;
import com.lap.auth.application.tenant.command.CreateTenantCmd;
import com.lap.auth.application.tenant.command.UpdateTenantCmd;
import com.lap.auth.application.tenant.dto.QueryTenantPageQuery;
import com.lap.auth.application.tenant.dto.TenantDto;
import com.lap.auth.application.tenant.repository.TenantQueryRepository;
import com.lap.auth.domain.tenant.Tenant;
import com.lap.auth.domain.tenant.repository.TenantRepository;
import com.lap.framework.common.dto.page.PageResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 应用服务层
 *
 * <ol>
 *   <li>不做业务规则，只做技术规则（加密/解密是技术实现，业务不关注）
 *   <li>取数，赋值，对象转换在这层执行
 *   <li>命名有规范，动词+领域名字
 *   <li>如查询比较多，建议分开领域QueryAppService,如:分页查询，分页查询返回关联字段
 * </ol>
 */
@RequiredArgsConstructor
@Service
public class TenantAppService {

  private final TenantAssembler tenantAssembler;
  private final TenantRepository repository;
  private final TenantQueryRepository queryRepository;

  public PageResponse<TenantDto> queryTenantsByPage(QueryTenantPageQuery query) {
    return queryRepository.findByConditionPaged(query);
  }

  public Integer createTenant(CreateTenantCmd cmd) {
    Tenant tenant = tenantAssembler.toDomain(cmd);
    return repository.save(tenant);
  }

  public Integer updateTenant(Integer id, UpdateTenantCmd cmd) {
    // TODO 这里校验是否存在
    Tenant tenant = tenantAssembler.toDomain(id, cmd);
    return repository.save(tenant);
  }

  public Integer deleteTenant(Integer id) {
    // TODO 这里校验是否存在
    return repository.deleteById(id);
  }
}
